﻿using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using Repetition.Domain;
using Repetition.Persistence;
using Repetition.Persistence.NHibernate;

namespace Repetition.DatabaseCreator
{
    public class DatabaseCreator
    {
        public static void Create()
        {
            Configuration configuration = new Configuration()
                .Configure()
                .AddAssembly(typeof(Repetition.Persistence.NHibernate.PersistentContext).Assembly);

            new SchemaExport(configuration).Execute(true, true, false, true);
        }

        public static void AddTestData()
        {
            var security = new Subject("Security");
            var networks = new Subject("Networks");
            var os = new Subject("Operating systems");
            var p1 = new Problem("What kinds of inspection are usually done by firewalls?", "Packet, application and DoS inspection");
            var p2 = new Problem("Name three common security features other than inspection that are often used by firewalls!", "NAT, VPN and authorization");
            var p3 = new Problem("What kind of problem is illustrated by the dining philosophers problem?", "Deadlocks");
            p1.AddSubject(security);
            p1.AddSubject(networks);
            p2.AddSubject(security);
            p2.AddSubject(networks);
            p3.AddSubject(os);

            using (ISession session = SessionFactorySingleton.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(p1);
                    session.Save(p2);
                    session.Save(p3);
                    transaction.Commit();
                }
            }
        }
    }
}
